package com.cqc.jdk8.Jutil.function;

import com.cqc.other.methodContest.Person;
import org.junit.Test;

import java.util.function.Consumer;

/**
 * Consumer 消费者  有参无返回值
 */
public class ConsumerTest {


    @Test
    public void f1() {
//        Consumer<Person> c=(p)-> System.out.println(p.getUsername());
        //或者
        Consumer<Person> c = System.out::println;
        c.accept(new Person("mike", 18));
    }

    @Test
    public void f2() {
        //开始
        //消费者
        //结束
        f2_1(x -> {
            System.out.println("消费者");
        });
    }

    public void f2_1(Consumer<String> consumer) {
        System.out.println("开始");
        consumer.accept("A");
        System.out.println("结束");
    }


    @Test
    public void f3() {
        //开始
        //HELLO WORD
        //hello word
        //结束
        f3_1((x) -> {
            System.out.println(x.toUpperCase());
        }, x -> {
            System.out.println(x.toLowerCase());
        });
    }

    public void f3_1(Consumer<String> consumer1, Consumer<String> consumer2) {
        System.out.println("开始");
        String str = "hello word";
        consumer1.accept(str);
        consumer2.accept(str);
        System.out.println("结束");
    }


    @Test
    public void f4() {
        //开始
        //HELLO WORD
        //hello word
        //结束
        f4_1((x) -> {
            System.out.println(x.toUpperCase());
        }, x -> {
            System.out.println(x.toLowerCase());
        });
    }

    public void f4_1(Consumer<String> consumer1, Consumer<String> consumer2) {
        System.out.println("开始");
        String str = "hello word";
        consumer1.andThen(consumer2).accept(str);
        System.out.println("结束");
    }
}
